<template>
  <div>
    <el-dialog
      :title="info.idAdd ? '添加菜单' : '编辑菜单'"
      :before-close="cancel"
      :visible.sync="info.isShow"
      center
    >
      <el-form :model="menuForm" ref="menu" :rules="rules">
        <!-- 第一个input  菜单名称-->
        <el-form-item
          label="菜单名称"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-input v-model="menuForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <!-- 第二个input 多选 -->
        <el-form-item label="上级菜单" :label-width="formLabelWidth">
          <el-select v-model="menuForm.pid" placeholder="请选择">
            <el-option :value="0" label="顶级菜单"> </el-option>
            <el-option
              v-for="item in menulist"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 单选 -->
        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio v-model="menuForm.type" :label="1">目录</el-radio>
          <el-radio v-model="menuForm.type" :label="2">菜单</el-radio>
        </el-form-item>
        <!-- 菜单图标 -->
        <el-form-item v-if="menuForm.type==1" label="菜单图标" :label-width="formLabelWidth">

           <el-select v-model="menuForm.icon" placeholder="请选择">
            <el-option
              v-for="item in iconlist"
              :key="item"
              :value="item"
              :label="item"
            ></el-option>
          </el-select>

        </el-form-item>
        <!-- 菜单地址 -->
        <el-form-item v-else label="菜单地址" :label-width="formLabelWidth">
          <el-input v-model="menuForm.url" autocomplete="off"></el-input>
        </el-form-item>
        <!-- 状态 -->
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="menuForm.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="addInfo" v-if="info.isAdd" type="primary"
          >添加</el-button
        >
        <el-button @click="editInfo" v-else type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {mapActions,mapGetters} from 'vuex'
import { menuAdd, menuInfo,menuEdit } from "../../util/request/api";
export default {
  props: ["info"],
  data() {
    return {
      formLabelWidth: "120px",
      //   表单的数据
      menuForm: {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      },
      // options: [
      //   {
      //     value: "1",
      //     label: "系统管理",
      //   },
      //   {
      //     value: "2",
      //     label: "商城管理",
      //   },
      // ],
      rules: {
        title: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" },
        ],
      },
            iconlist: [
        "el-icon-s-tools",
        "el-icon-setting",
        "el-icon-s-goods",
        "el-icon-goods",
      ],
    };
  },
  computed:{
    ...mapGetters({
      menulist: "menu/getMenuList",
    })
  },
  methods: {
    // 重新获取列表
    // getMenuList() {
    //   menuList().then((res) => {
    //     console.log(res, "菜单列表 ");
    //     // 如果通过，那么就获取数据库里的列表
    //     if (res.code == 200) {
    //       this.tableData = res.list;
    //     }
    //   });
    // },
            ...mapActions({
      getMenulist: "menu/getMenuAction",
    }),
    //   关闭弹窗
    cancel() {
      // 子传父
      this.$emit("cancel", false);
      // 不可以是双引号！！！！！！！！！！！！！！！
      // this.$emit('cancel',false)
      this.empty();
    },
    // 清空弹窗
    empty() {
      this.menuForm = {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      };
    },
    // 为列表添加一条信息
    addInfo() {
      // console.log(this.menuForm);
      this.$refs.menu.validate((valid) => {
        if (valid) {
          // 调取接口
          console.log(this.menuForm);

          menuAdd(this.menuForm).then((res) => {
            console.log(res, "菜单");
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel();
              // 刷新一次，更新列表
              // location.reload(),单页应用不使用
            this.getMenulist()
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
        }
      });
    },
    // 获取一条数据e
    lookup(id){
     console.log(id,'每一条数据的主key');
     menuInfo({id})
     .then(res=>{
       console.log(res,'每一条数据的主key');
       this.menuForm = res.list
       this.menuForm.id = id
     })
    },
        editInfo() {
      // console.log(this.menuForm);
      this.$refs.menu.validate((valid) => {
        if (valid) {
          // 调取接口
          console.log(this.menuForm);

          menuEdit(this.menuForm).then((res) => {
            console.log(res, "菜单");
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel();
              // 刷新一次，更新列表
              // location.reload(),单页应用不使用
            this.getMenulist()
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
        }
      });
    },
  },
};
</script>